<?php
namespace app\walletyhbbadmin\controller;
use Think\Db;
use think\Collection;
use Think\page;
use think\Session;
class Wuliu extends AdminBase
{
    //空操作
    public function _empty()
    {
        header("HTTP/1.0 404 Not Found");
        $this->display('Public:404');
    }

    /**
     * 配送物流列表
     */
    public function index()
    {
        //查表加分页
        $shipping_name=input('shipping_name');
        if(!empty($shipping_name)){
            $where['shipping_name']=array('like','%'.$shipping_name.'%');
        }
        $wl = DB::name("shipping")->where($where)->paginate(5, false, ['query' => input()]);
        $page = $wl->render();
        $wl = $wl->all();
        $this->assign('page', $page);//赋值
        $this->assign('wl', $wl);
        return $this->fetch();
    }

    /**
     * 添加配送物流页面
     */
    public function addWuliu()
    {
        return $this->fetch('wuliu/add');
    }

    /*
     *执行添加配送物流
     **/
    public function insertwuliu()
    {
        //获取input里的值
        $request = request();
        $data['shipping_code'] = input('shipping_code');//快递代号
        $data['shipping_name'] = input('shipping_name');//快递名称
        $data['shipping_desc'] = input('shipping_desc');//描述
        $data['insure'] = input('insure');//保险
        $data['enabled'] = input('enabled');//状态
        //判断添加不能为空
        if (empty($data['shipping_code,shipping_name,shipping_desc,insure'])&&$data['insure']!=='0') {
            $this->error('请补全信息', '/walletyhbbadmin/Wuliu/addWuliu');
        }
        //添加数据库
        $bool = DB::name('shipping')->insert($data);
        //判断是否上传成功
        if ($bool) {
            $this->success('上传成功', U('Wuliu/index'));
        } else {
            $this->error('上传失败', '/walletyhbbadmin/Wuliu/addWuliu');
        }
    }


    /**
     * 修改物流上架下架状态
     */
    public function updateStatus()
    {
        //获取id
        $shipping_id = input('shipping_id');
        //查库
        $goods_status = Db::name('shipping')->where(array('shipping_id' => $shipping_id))->find();
        if ($goods_status['enabled'] == 1) {
            $data['enabled'] = 0;
        } else {
            $data['enabled'] = 1;
        }
        $res = Db::name('shipping')->where(array('shipping_id' => $shipping_id))->update($data);
        if ($res) {
            $this->success('修改成功');
        } else {
            $this->error('参数错误');
        }
    }
    /**
     * 删除配送物流
     */
    public function delWuliu(){
        //获取id
        $shipping_id=input('shipping_id');
        $where['shipping_id'] = input('shipping_id');
        //删除库对应ID
        $res=Db::name('shipping')->where($where)->delete();
        //判断是否删除成功
        if($res){
            $data['info']='删除成功';
            $data['status']='1';
            return json($data);
        }else{
            $data['info']='删除失败';
            $data['status']='-1';
            return json($data);
        }
    }

    /*
     *修改物流页面
     **/
    public function edit(){
        //获取ID
        $where['shipping_id'] = input('shipping_id');
        //查库
        $info = DB::name('shipping')->where($where)->find();
        $this->assign('info',$info);//赋值
        return $this->fetch('wuliu/edit');
    }

    /*
     *执行修改物流页面
     **/
    public function updateWuliu(){
        $request = request();
        $where['shipping_id'] = input('shipping_id');
        $info = DB::name('shipping')->where($where)->find();
        $data['shipping_code'] = input('shipping_code');//快递代号
        $data['shipping_name'] = input('shipping_name');//快递名称
        $data['shipping_desc'] = input('shipping_desc');//物流描述
        $data['insure'] = input('insure');//保险
        $data['enabled'] = input('enabled');//状态
        //修改后添加到数据库
        $bool = DB::name('shipping')->where($where)->update($data);
        //判断是否修改成功
        if($bool){
            $this->success('修改成功','/walletyhbbadmin/Wuliu/index');
        }else{
            $this->error('修改失败');
        }
    }


    /**
     * 用户地址表
     */
    public function yonghudizhi()
    {
        $consignee=input('consignee');
        if(!empty($consignee)){
            $where['consignee']=array('like','%'.$consignee.'%');
        }
        $user_id=input('user_id');
        if(!empty($user_id)){
            $where['user_id']=array('like','%'.$user_id.'%');
        }
        $mobile=input('mobile');
        if(!empty($mobile)){
            $where['mobile']=array('like','%'.$mobile.'%');
        }
        $res = Db::name('region')->where('parent_id',0)->select();
        $yh = DB::name("user_address")->where($where)->paginate(20, false, ['query' => input()]);
        $page = $yh->render();
        $yh = $yh->all();
        foreach($yh as $k=>$v){
            $sheng=Db::name("region")->where('id',$v['province'])->find();
            $shi=Db::name("region")->where('id',$v['city'])->find();
            $qu=Db::name("region")->where('id',$v['district'])->find();
            $yh[$k]['sheng']=$sheng['name'];
            $yh[$k]['shi']=$shi['name'];
            $yh[$k]['qu']=$qu['name'];
        }
        $this->assign('page', $page);
        $this->assign('yh', $yh);
        $this->assign('res',$res);
        return $this->fetch('wuliu/yonghu');
    }

    /**
     * 删除用户地址
     */
    public function delYonghu(){
        //获取id
        $shipping_id=input('address_id');
        $where['address_id'] = input('address_id');
        //删除库对应ID
        $res=Db::name('user_address')->where($where)->delete();
        //判断是否删除成功
        if($res){
            $data['info']='删除成功';
            $data['status']='1';
            return json($data);
        }else{
            $data['info']='删除失败';
            $data['status']='-1';
            return json($data);
        }
    }

    /*
    *修改用户地址页面
    **/
    public function edityonghu(){
        //获取ID
        $where['address_id'] = input('address_id');
        //查库
        $info = DB::name('user_address')->where($where)->find();
        $sheng=Db::name("region")->where('level','1')->select();
        $wheres['level'] = 2;
        $wheres['parent_id'] = $info['province'];
        $shi = DB::name('region')->where($wheres)->select();
        $res['level'] = 3;
        $res['parent_id'] = $info['city'];
        $qu = DB::name('region')->where($res)->select();
        $province = DB::name('region')->where ( array('parent_id'=>1) )->select();
        //赋值
        $this->assign('info',$info);
        $this->assign('sheng',$sheng);
        $this->assign('shi',$shi);
        $this->assign('qu',$qu);
        $this->assign('province',$province);
        return $this->fetch('wuliu/edityonghu');
    }

    /*
     *执行修改用户地址页面
     **/
    public function updateYonghu(){
        $request = request();
        //获取ID
        $where['address_id'] = input('address_id');
        //查库
        $info = DB::name('user_address')->where($where)->find();
        //获取input里修改后的值
        $data['consignee'] = input('consignee');//收货人
        $data['email'] = input('email');//邮箱地址
        $data['country'] = input('country');//国家
        $data['province'] = input('province');//省
        $data['city'] = input('city');//市
        $data['district'] = input('district');//县
        $data['address'] = input('address');//地址
        $data['zipcode'] = input('zipcode');//邮政编码
        $data['mobile'] = input('mobile');//手机
        $data['enabled'] = input('enabled');//状态
        //修改后添加到数据库
        $bool = DB::name('user_address')->where($where)->update($data);
        //判断是否修改成功
        if($bool){
            $this->success('修改成功','/walletyhbbadmin/Wuliu/yonghudizhi');
        }else{
            $this->error('修改失败');
        }
    }

    /**
     * 城市联动获取方法
     */
    public function getLevel(){
        //获取ID
        $id = input('id');
        $where['parent_id'] = $id;
        //查库
        $list = Db::name('region')->where($where)->select();
        if($list){
            $re['status'] = 1;
            $re['data'] = $list;
        }else{
            $re['status'] = 0;
        }
        return json($re);
    }

    /**
     * 配送物流区域地址表
     */
    public function wuliuquyu()
    {
        //查库加分页
        $qy = DB::name("shipping_area ")->paginate(5, false, ['query' => input()]);
        $page = $qy->render();
        $qy = $qy->all();
        //赋值
        $this->assign('page', $page);
        $this->assign('qy', $qy);
        return $this->fetch('wuliu/wuliuquyu');
    }

    /**
     * 添加物流区域页面
     */
    public function addWuliuquyu()
    {
        return $this->fetch('wuliu/addquyu');
    }

    /*
     *添加配送物流
     **/
    public function insertwuliuquyu()
    {
        $request = request();
        //获取input里的内容
        $data['shipping_area_name'] = input('shipping_area_name');//配送区域名称
        $data['shipping_code'] = input('shipping_code');//物流id
        $data['update_time'] = time();//时间
        $data['is_default'] = input('is_default');//状态
        //判断添加信息
        if (empty($data['shipping_area_name'])) {
            $this->error('请补全信息', '/walletyhbbadmin/Wuliu/addWuliuquyu');
        }
        if (empty($data['shipping_code'])) {
            $this->error('请补全信息', '/walletyhbbadmin/Wuliu/addWuliuquyu');
        }
        //添加到数据库
        $bool = DB::name('shipping_area')->insert($data);
        //判断是否添加成功
        if ($bool) {
            $this->success('添加成功', U('Wuliu/wuliuquyu'));
        } else {
            $this->error('添加失败', '/walletyhbbadmin/Wuliu/addWuliuquyu');
        }
    }

    /**
     * 修改物流区域上架下架状态
     */
    public function updateWuliuquyu()
    {
        //获取id
        $shipping_area_id = input('shipping_area_id');
        //查库
        $goods_status = Db::name('shipping_area')->where(array('shipping_area_id' => $shipping_area_id))->find();
        //判断状态
        if ($goods_status['is_default'] == 1) {
            $data['is_default'] = 0;
        } else {
            $data['is_default'] = 1;
        }
        //修改数据库
        $res = Db::name('shipping_area')->where(array('shipping_area_id' => $shipping_area_id))->update($data);
        //判断是否修改成功
        if ($res) {
            $this->success('修改成功');
        } else {
            $this->error('参数错误');
        }
    }

    /**
     * 删除物流区域
     */
    public function delWuliuquyu(){
        //获取id
        $shipping_area_id=input('shipping_area_id');
        $where['shipping_area_id'] = input('shipping_area_id');
        //删除库对应ID
        $res=Db::name('shipping_area')->where($where)->delete();
        //判断是否删除成功
        if($res){
            $data['info']='删除成功';
            $data['status']='1';
            return json($data);
        }else{
            $data['info']='删除失败';
            $data['status']='-1';
            return json($data);
        }
    }

    /*
   *修改物流区域页面
   **/
    public function editquyu(){
        //获取ID
        $where['shipping_area_id'] = input('shipping_area_id');
        //查库
        $info = DB::name('shipping_area')->where($where)->find();
        //赋值
        $this->assign('info',$info);
        //页面跳转
        return $this->fetch('wuliu/editquyu');
    }

    /*
     *执行修改物流区域页面
     **/
    public function updateQuyu(){
        $request = request();
        //获取ID
        $where['shipping_area_id'] = input('shipping_area_id');
        //查库
        $info = DB::name('shipping_area')->where($where)->find();
        //获取input里的值
        $data['shipping_area_name'] = input('shipping_area_name');//配送区域名称
        $data['shipping_code'] = input('shipping_code');//物流id
        $data['is_default'] = input('is_default');//状态
        //修改后添加到数据库
        $bool = DB::name('shipping_area')->where($where)->update($data);
        //判断是否修改成功
        if($bool){
            $this->success('修改成功','/walletyhbbadmin/Wuliu/wuliuquyu');
        }else{
            $this->error('修改失败');
        }
    }
}